package com.yykj.user.center.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yykj.common.vo.ResultVO;
import com.yykj.user.center.client.dto.MedicalRecordBaseInfoDTO;
import com.yykj.user.center.server.dto.MedicalRecordInfoDTO;
import com.yykj.user.center.server.emun.CustomMessageType;
import com.yykj.user.center.server.form.AddMedicalRecordForm;
import com.yykj.user.center.server.model.ConsultationInfoOrder;
import com.yykj.user.center.server.service.MedicalRecordService;
import com.yykj.user.center.server.service.patient.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenl
 * @version 1.0
 * @date 2021/11/13 16:58
 */

@RestController
@RequestMapping("/api/medicalRecord")
@Api(tags = "病历相关接口")
public class MedicalRecordController {
    @Autowired
    private MedicalRecordService medicalRecordService;

    @Autowired
    private PaymentService paymentService;

    @PostMapping("/create")
    @ApiOperation(value = "编写病历", notes = "编写病历")
    public ResultVO create(@RequestBody @Valid AddMedicalRecordForm addMedicalRecordForm) {
        ConsultationInfoOrder consultationInfoOrder = medicalRecordService.create(addMedicalRecordForm);
        if (consultationInfoOrder != null) {
            Map<String, Object> finishMap = new HashMap();
            finishMap.put("type", CustomMessageType.orderFinish.getCode());
            finishMap.put("data", consultationInfoOrder.getId());
            paymentService.sendPayedMsg(consultationInfoOrder.getDoctorTencentAccountId(), consultationInfoOrder.getPatientTencentAccountId(), JSON.toJSONString(finishMap, SerializerFeature.WriteNullStringAsEmpty), 1);
            return ResultVO.success(true);
        }
        return ResultVO.error(null);
    }

    @GetMapping("/medicalInfo")
    @ApiOperation(value = "病历详情", notes = "病历详情")
    public ResultVO<MedicalRecordInfoDTO> medicalInfo(@RequestParam(value = "id", required = true) String id) {
        return ResultVO.success(medicalRecordService.medicalInfo(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "startDate", value = "开始日期", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", required = false, dataType = "String"),
            @ApiImplicitParam(name = "doctorName", value = "医生姓名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "patientName", value = "患者姓名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "订单编号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderType", value = "订单类型：11、图文问诊 12、语音问诊 13、视频问诊 14、检查订单 15、检验订单", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "endType", value = "结束方式：0手动结束 自动结束", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "state", value = "状态：1待接诊  2问诊中  3已完成", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "refundStatus", value = "患者退款状态-1:未退款,0:申请退款,1:已退款,2:退款失败", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "payStatus", value = "患者支付状态-1:取消,0:待支付,1:已支付", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "hospitalId", value = "医院Id", required = false, dataType = "Integer"),
    })

    @ApiOperation(value = "电子病历列表", notes = "电子病历列表")
    @GetMapping("/list")
    public ResultVO<List<MedicalRecordBaseInfoDTO>> list(@RequestParam(value = "userId", required = false) String userId, @RequestParam(value = "patientCardId", required = false) String patientCardId) {
        return ResultVO.success(medicalRecordService.list(userId, patientCardId));
    }

}
